﻿Imports FinanceiroBLL
Imports FinanceiroDAL
Imports FinanceiroDAL.ModuloConexao
Public Class ContasMovimentoFRM
    Dim objBLL As New ContasBLL
    Dim dtpag As Date
#Region "Load"
    Private Sub ContasMovimentoFRM_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        objconexao.carregaCombo(CBContas, "Financeiro.Ofi_Contas", "con_codigo", "con_descricao", "sta_codigo=1", "")
        CBContas.SelectedIndex = -1
    End Sub
#End Region

#Region "Key Press"
    Private Sub CBContas_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles CBContas.KeyPress
        e.Handled = True
    End Sub
#End Region
#Region "SomaGrid"
    Sub somagrid()
        Try
            Dim valor As Decimal = 0
            Dim Debito As Decimal = 0
            Dim credito As Decimal = 0
            For Each col As DataGridViewRow In DGVDados.Rows
                If col.Cells("icp_valorpago").Value < 0 Then
                    Debito = Debito + col.Cells("icp_valorpago").Value
                End If
                If col.Cells("icp_valorpago").Value > 0 Then
                    credito = credito + col.Cells("icp_valorpago").Value
                End If
                valor = valor + col.Cells("icp_valorpago").Value
            Next
            TXTTotalGrid.Text = valor 'Math.Round(valor, 2, MidpointRounding.AwayFromZero)
            TXTTotalGrid.Text = (valor.ToString.Replace("R$", ""))
            TXTTotalGrid.Text = FormatCurrency(TXTTotalGrid.Text)
            Me.LBNumRegistros.Text = DGVDados.RowCount.ToString 'linhas

            TXTCredito.Text = credito
            TXTCredito.Text = credito.ToString.Replace("R$", "")
            TXTCredito.Text = FormatCurrency(TXTCredito.Text)

            TXTDebito.Text = Debito
            TXTDebito.Text = Debito.ToString.Replace("R$", "")
            TXTDebito.Text = FormatCurrency(TXTDebito.Text)
        Catch ex As Exception
            Throw New Exception("Ocorreu um erro na soma!")
        End Try
        ' DGVDados.ColumnHeadersDefaultCellStyle.ForeColor = Color.Red (esta função colore de verm os titulos da coluna)
    End Sub
#End Region
#Region "Close"
    Private Sub TSBFechar_Click(sender As System.Object, e As System.EventArgs) Handles TSBFechar.Click
        Me.Close()
        objBLL = Nothing
    End Sub
#End Region
#Region "Pre-Paint"
    Private Sub DGVDados_RowPrePaint(sender As System.Object, e As System.Windows.Forms.DataGridViewRowPrePaintEventArgs) Handles DGVDados.RowPrePaint
        If DGVDados.Rows.Count > 0 Then
            If (DGVDados.Rows(e.RowIndex).Cells("icp_valorpago").Value > 0) Then
                Me.DGVDados.Rows(e.RowIndex).DefaultCellStyle.ForeColor = Color.Green

            End If
            If (DGVDados.Rows(e.RowIndex).Cells("icp_valorpago").Value < 0) Then
                Me.DGVDados.Rows(e.RowIndex).DefaultCellStyle.ForeColor = Color.Red
            End If
        End If
    End Sub
#End Region
#Region "TSB Novo"
    Private Sub TSBNovo_Click(sender As System.Object, e As System.EventArgs) Handles TSBEntrada.Click
        Dim frm As New ContasLançamentosFRM
        TSBEntrada.BackColor = Color.Gainsboro
        frm.ShowDialog()
        TSBEntrada.BackColor = Color.Transparent
        frm.Dispose()
        frm = Nothing
    End Sub
#End Region
#Region "Buscar"
    Private Sub BBuscar_Click(sender As System.Object, e As System.EventArgs) Handles BBuscar.Click
        Dim opt As Integer = 0

        Try
            objBLL = New ContasBLL
            opt = DateTime.Compare(DTPInicial.Value, DTPFinal.Value)
            If opt <= 0 Then
                DGVDados.DataSource = objBLL.localizaContaPeriodo(CBContas.SelectedValue, DTPInicial.Value, DTPFinal.Value)
                somagrid()
                DGVDados.Refresh()
                TSBRelatorio.Enabled = True
                objBLL = Nothing
            Else
                MsgBox("Data inicial maior que a data final")
                DTPInicial.Select()
            End If
        Catch ex As Exception
        End Try
    End Sub
#End Region
#Region "TSBSaída"
    Private Sub TSBSaida_Click(sender As System.Object, e As System.EventArgs) Handles TSBSaida.Click
        Dim frm As New ContasaPagarFRM
        TSBEntrada.BackColor = Color.Gainsboro
        frm.Op = "M"
        frm.ShowDialog()
        TSBEntrada.BackColor = Color.Transparent
        frm.Dispose()
        frm = Nothing

    End Sub
#End Region
#Region "BTNtransferir"
    Private Sub TSBTransferir_Click(sender As System.Object, e As System.EventArgs) Handles TSBTransferir.Click
        Dim objtrans As New TransfereFRM
        objtrans.ShowDialog()
        objtrans.Dispose()
        objtrans = Nothing
    End Sub
#End Region
#Region "Saldo atual"
    Private Sub TSBSaldoatual_Click(sender As System.Object, e As System.EventArgs) Handles TSBSaldoatual.Click
        GBPeriodo.Enabled = False
        Dim objconta As New ContasBLL
        Dim opt As Integer = 0

        Try
            opt = DateTime.Compare(DTPInicial.Value, DTPFinal.Value)
            If opt <= 0 Then
                If TSBSaldoatual.BackColor <> Color.Gainsboro Then
                    If CBContas.SelectedValue <> Nothing Then
                        objconta = New ContasBLL(CBContas.SelectedValue)
                        Dim dtabertura As Date = objconta.dtabertura.ToShortDateString()
                        TSBSaldoatual.BackColor = Color.Gainsboro
                        objBLL = New ContasBLL
                        DGVDados.DataSource = objBLL.localizaContaPeriodo(CBContas.SelectedValue, dtabertura, DTPFinal.Value)
                        somagrid()
                        DGVDados.Refresh()
                        CBContas.SelectedValue = -1
                        TSBRelatorio.Enabled = True
                        objBLL = Nothing
                    Else
                        MsgBox("Selecione uma conta")
                        CBContas.SelectAll()
                    End If
                Else
                    TSBSaldoatual.BackColor = Color.Transparent
                    GBPeriodo.Enabled = True
                    DGVDados.Refresh()
                End If
            Else
                MsgBox("Data Inicial maior que a data final")
                DTPInicial.Select()
            End If
        Catch ex As Exception
        Finally
        End Try
    End Sub
#End Region
    Private Sub TSBDetalhes_Click(sender As System.Object, e As System.EventArgs)

        Dim frm As New RelatoriosfinFRM
        Dim rpt As New ContaDetalhadaREL
        Dim opt As Integer = 0
        GBPeriodo.Enabled = True
        Try
            opt = DateTime.Compare(DTPInicial.Value, DTPFinal.Value)

            If CBContas.SelectedValue <> 0 Then
                If opt <= 0 Then
                    rpt.SetDatabaseLogon(usuario, senha)
                    rpt.SetParameterValue(0, "Relatório de Conta Detalhado")
                    rpt.SetParameterValue(1, CBContas.SelectedValue)
                    rpt.SetParameterValue(2, DTPInicial.Value.ToString("yyyy-MM-dd"))
                    rpt.SetParameterValue(3, DTPFinal.Value.ToString("yyyy-MM-dd"))
                    frm.CrystalReportViewer2.ToolPanelView = CrystalDecisions.Windows.Forms.ToolPanelViewType.None
                    frm.CrystalReportViewer2.ShowGroupTreeButton = False
                    frm.CrystalReportViewer2.ReportSource = rpt
                    frm.ShowDialog()
                    'Next
                Else
                    MsgBox("Data inicial maior que a data final!")
                End If
            Else
                MsgBox("Selecione uma conta", MsgBoxStyle.Exclamation)
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
        frm.Dispose()
        rpt = Nothing
    End Sub
    Private Sub TSBRelatorio_Click(sender As System.Object, e As System.EventArgs) Handles TSBRelatorio.Click
        TSBDetalhes_Click(Nothing, e)
    End Sub
End Class